package com.runa.rccprotocol.analysis.wukexing;

import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.persistence.DateUtil;
import com.runa.protocol.dto.IInterData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import com.runa.rccprotocol.dto.runa.RunaICStruct;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 2024/11/21 9:38
 * zhangYB
 */
@Data
public class WkxAnalysis {

    public WkxAnalysis() {
    }

    protected IInterData interData;

    public int handleWkxValve(byte[] data, int begin, int length) {
        boolean v = false;
        for (int i = begin; i < length; i++) {
            if ((data[i] & 0xff) == 0x68 && (data[i + 1] & 0xff) == 0x80) {
                begin = i;
                v = true;
                break;
            }
        }
        if (!v) {
            return 0;
        }
        int len = data[begin + 10];
        if (begin + 11 + len + 2 > length) {
            return -1;
        }
        if (data[begin + 11 + len] != ByteUtil.sum(data, begin, 11 + len)) {
            return 0;
        }
        int c = (data[begin + 9] & 0xff);
        switch(c) {
            case 0x81:
                return handleReadValve(data, begin, length);
            case 0x84:
                this.interData = new RunaConcenAffirmDeny(true);
        }
        return 0;
    }

    private int handleReadValve(byte[] data, int begin, int length) {
        ValveVO valveVO = new ValveVO();
        valveVO.setReadTime(new Date());
        begin += 2;
        String serial = ByteUtil.byteToHexStringFlip(data, begin, 4);
        valveVO.setValveSerial(serial);
        begin += 7;
        begin += 5;
        valveVO.setOpenTime(Float.valueOf(ByteUtil.byteToHexStringFlip(data, begin, 3)));
        begin += 3;
        begin += 3;
        begin += 3;
        begin += (2 * 3);
        valveVO.setSupplyTemp((float)(Float.valueOf(ByteUtil.byteToHexStringFlip(data, begin, 2)) * 0.01));
        begin += 2;
        valveVO.setReturnTemp((float)(Float.valueOf(ByteUtil.byteToHexStringFlip(data, begin, 2)) * 0.01));
        begin += 2;
        begin += 2;
        begin += 4;
        valveVO.setValveDate(DateUtil.stringToDate(ByteUtil.byteToHexStringFlip(data, begin, 5), "yyMMddHHmm"));
        begin += 5;
        valveVO.setSoftVersion(ByteUtil.byteToHexString(data, begin, 2));
        begin += 2;
        valveVO.setStatus(ByteUtil.byteToHexString(data, begin, 2));
        byte vst4 = data[begin++];
        byte vst3 = data[begin++];
        byte vst2 = data[begin++];
        byte vst1 = data[begin++];
        valveVO.setActOpen((float)(vst1 & 0x7f));
        List<String> faultList = new ArrayList<>();
        if ((vst4 >> 6 & 0b11) == 0b11) {
            faultList.add("温控器通讯异常");
        }
        if ((vst4 >> 5 & 0b1) == 0b1) {
            faultList.add("温控器被移动");
        }
        if ((vst4 >> 4 & 0b1) == 0b1) {
            faultList.add("低电压");
        }
        valveVO.setFaultCode(faultList);
        RunaConcenResponseData runaConcenResponseData = new RunaConcenResponseData();
        runaConcenResponseData.addRepData(valveVO);
        this.interData = runaConcenResponseData;
        return 0;
    }



}
